package com.fh.shop.api.mq.consumer;

import com.fh.shop.api.mq.config.MQConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class HelloConsumer {

    @RabbitListener(queues = MQConstant.Test.QUEUE)
    public void handleHelloMsg(String msg, Message message, Channel channel) throws IOException {
        //获取一个 标志位
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("消息：{}",msg);
        if (msg.contains("1")){
            System.out.println(1 / 0);
        }
            System.out.println("==============");
        } catch (Exception e) {
            e.printStackTrace();
            // 将异常消息，记录到数据库中，进行人工处理
            log.error("消息异常：{}",msg);
        } finally {
            //响应 ack，将消息从队列中删除  第二个参数，false，只在队列中删除一次即可
            channel.basicAck(deliveryTag,false);
        }
    }


    @RabbitListener(queues = MQConstant.TestFanout.QUEUE1)
    public void handleFanoutMsg1(String msg){
        log.info("队列1：{}",msg);
    }

    @RabbitListener(queues = MQConstant.TestFanout.QUEUE2)
    public void handleFanoutMsg2(String msg){
        log.info("队列2：{},{}",msg,"msg");
    }
}
